
local pool=class("pokepool")

function pool:initData()
    local spool={}
    spool[1]={"poke__yibu","poke__baibianguai", "poke__biketini", "poke__chaomeng",
 "poke__chenglong", "poke__dayanshe","poke__dianxi", "poke__guoranwen"
 , "poke__huashiyilong", "poke__jidongniao", "poke__jielaaola",
  "poke__kabishou", "poke__kailudiou", "poke__kedaya", 
  "poke__kuailong",   "poke__meiluoyeta", 
  "poke__menghuan", "poke__miaomiao",
  "poke__pikaqiu", "poke__shandianniao",
    }

    spool[2]={
   "poke__dujiaochong","poke__dujiaochong2","poke__guisi","poke__guisi2"
   ,"poke__jienigui","poke__jienigui2",   "poke__kaixi","poke__kaixi2", "poke__labaya","poke__labaya2", "poke__labaya3",
  "poke__lvmaochong","poke__lvmaochong2","poke__miaowazhongzi","poke__miaowazhongzi2", 
  "poke__minilong","poke__minilong2","poke__niduolan3", "poke__niduona","poke__niduona2",
 "poke__tiejiayong","poke__tiejiayong2","poke__tiekeyong","poke__tiekeyong2","poke__wanli","poke__wanli2",
 "poke__wenxiangkedou","poke__wenxiangkedou2","poke__wenxiangkedou3","poke__xiaohuolong","poke__xiaohuolong2","poke__xiaoquanshi","poke__zoulucao","poke__zoulucao2","poke__zoulucao3"
    
    }
    spool[3]={"poke__chouchouhua", "poke__huokonglong" , "poke__bibiniao", "poke__guisitong", "poke__kamigui", "poke__haoli",
"poke__yongjila","poke__wenxiangwa","poke__niduolan", "poke__miaowacao", "poke__longlongshi",  "poke__yongjila2","poke__niduolan2"
,"poke__miaowacao2","poke__koudaihua","poke__koudaihua2","poke__kamigui2","poke__huokonglong2", "poke__haoli2","poke__chouchouhua2",
"poke__wenxiangwa2","poke__hakelong","poke__hakelong2","poke__guisitong2",}

    spool[4]={"poke__badadie","poke__genggui", "poke__bawanghua", "poke__bidiao",
 "poke__guaili", "poke__dashihua", "poke__dazhenfeng",  "poke__wenxiangyongshi", "poke__shuijiangui","poke__niduohou", "poke__penhuolong"
 , "poke__miaowahua",  "poke__longlongyan"
    }
self.totalpool=spool

end
--从池子里抽数目的怪,完后删除对应卡牌
function pool:drawone(level,num)
    local num=num or 1

    local smpool=self.totalpool or {}
    local list=smpool[level]
    
    if #list==0 then return {} end
    local len=math.min(#list,num)
    local ret
    --print(list[1])
    ret=table.random(list,len)
    for _,v in ipairs(ret) do
        table.removeOne(list,v)
    end
    return ret
end

--从游戏开始时池子里凑一组怪，每组抽4个,抽完后删除对应卡牌
function pool:startdraw()
    local ret={}
    for i=1,4 do
        local level=i
        local li=self:drawone(level,4)
        table.insert(ret,li)
    end
    return ret
end
--购买宝可梦后从池子里删除并更新新池子
function pool:shopuppokemon(room,cardname)
    local list=room.pool
    local stop=false
    for i=1,4 do
        for _, card in ipairs(list[i]) do
            if card==cardname then
                table.removeOne(list[i],card)
                local newcard=self:drawone(i,1)
                table.insertTable(list[i],newcard)
                stop=true
                break
            end
        end
        if stop then break end
    end
    --room.pool=list
end
--更新玩家宝石和宝可梦信息
function pool:updateplayergems(player, gemlist,cardlist)
    self:updateplayerbuff(player)
    player.gemlist=gemlist
    local cardlist=cardlist or player.owned_cards
    local bufflist=player.buff_table
    local list={}
    table.insert(list,gemlist)
    table.insert(list,cardlist)
    table.insert(list,bufflist)
    player.room:setPlayerMark(player,"@[jbspoke_gems]",list)
end

function pool:updateplayerbuff(player)
    local buff={0,0,0,0,0,0}
    
    if #player.owned_cards==0 then return end
    
    for _, card in ipairs(player.owned_cards) do
        if type(card[3])=="table" then
            for _,v in ipairs(card[3]) do
                local buffnum=v or 1
                buff[buffnum]=buff[buffnum]+1
            end
        else
            local buffnum=tonumber(card[3]) or 1
            buff[buffnum]=buff[buffnum]+1
        end
    end
    player.buff_table=buff
end

--玩家得分并判断是否结束
function pool:score(player,score)
    player.room:addPlayerMark(player, "@pokegems-score", score)
    local x=player:getMark("@pokegems-score")
    if x>=18 and not player.room:getTag("pokegems_end")then
        player.room:setTag("pokegems_end",true)
        player.room:setBanner("@@pokegems_end",1)

    end
end

    


--购买宝可梦同时更新宝石
function  pool:bugcard(player, card)
    -- player.gemlist 是一个长度为6的数组，表示玩家拥有的宝石数量，最后一位表示钻石卡的数量
    -- player.owned_cards 是一个包含玩家已购买卡牌名称的列表
    -- 计算玩家已购买的卡牌提供的buff
    local buff = player.buff_table  or {0,0,0,0,0,0}-- 初始化buff，表示每种宝石的buff数量
   
    local cost=card[2]
    -- 计算实际需要支付的宝石数量
    local actual_cost = {}
    for i = 1, 6 do
        actual_cost[i] = math.max(0, tonumber(cost[i]) - buff[i])
    end
   
    -- 优先使用普通宝石支付
    local diamonds_needed = 0
    for i = 1, 6 do
        if player.gemlist[i] >= actual_cost[i] then
            player.gemlist[i] = player.gemlist[i] - actual_cost[i]
            player.room.total_gems[i]=player.room.total_gems[i]+actual_cost[i]
        else
            diamonds_needed = diamonds_needed + (actual_cost[i] - player.gemlist[i])
            player.gemlist[i] = 0
        end
    end
    -- 使用钻石卡支付剩余的宝石
    if diamonds_needed > player.gemlist[6] then
        error("Not enough diamonds to pay for the card.")
    end
    
    player.gemlist[6] = player.gemlist[6] - diamonds_needed
    player.room.total_gems[6]=player.room.total_gems[6]+diamonds_needed
    
    -- 将卡牌添加到玩家的已购买卡牌列表中
    table.insert(player.owned_cards, card)
    -- 更新玩家信息
    local cardlist=table.map(player.owned_cards,function(card) return card[1] end)
   

    self:shopuppokemon(player.room,card[1])
    ---更新buff
    self:updateplayergems(player, player.gemlist,cardlist)--更新表
    pool:score(player,card[4])
   
end

--玩家宝石购买宝石同时更新总宝石
function pool:buggems(player,gemlist)

    local plist=player.gemlist
    for i=1,6 do
        local gem=tonumber(gemlist[i])
        plist[i]=gem+tonumber(plist[i])
        player.room.total_gems[i]=player.room.total_gems[i]-gem
    end
    local cardlist=table.map(player.owned_cards,function(card) return card[1] end)
    self:updateplayergems(player, plist,cardlist)
end

--玩家预定
function pool:gems(player,card,gemlist)

    local plist=player.gemlist
    for i=1,6 do
        local gem=tonumber(gemlist[i])
        plist[i]=gem+tonumber(plist[i])
        player.room.total_gems[i]=player.room.total_gems[i]-gem
    end

    self:updateplayergems(player, plist)
end



--玩家的选择；一个4种，购买三个不同球，2个相同球，购买一个怪，预定一个怪，根据传回来的卡表和球表确定选择
function pool:playerchoice(player,cardlist,balllist)
   if cardlist[1]~="chooseball" then 
    self:bugcard(player,cardlist)
   
   end
   self:buggems(player,balllist)
end


return pool